package com.puman.houmu.puman.Shouye;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.puman.houmu.puman.Fragment.GuideFragment;
import com.puman.houmu.puman.Fragment.HomeFragment;
import com.puman.houmu.puman.Fragment.MineFragment;
import com.puman.houmu.puman.Fragment.ShopFragment;
import com.puman.houmu.puman.R;
import com.startsmake.mainnavigatetabbar.widget.MainNavigateTabBar;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "HomeActivity";

    private static final String TAG_PAGE_HOME = "首頁";
    private static final String TAG_PAGE_SHOP = "商城";
    private static final String TAG_PAGE_ADD = "扩展";
    private static final String TAG_PAGE_GUIDE = "指南";
    private static final String TAG_PAGE_MINE = "我的";

    //构造PopupWindow
    private PopupWindow mPopWindow;
    private MainNavigateTabBar mainNavigateTabBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        mainNavigateTabBar = (MainNavigateTabBar) findViewById(R.id.mainTabBar);
        mainNavigateTabBar.onRestoreInstanceState(savedInstanceState);
        mainNavigateTabBar.addTab(HomeFragment.class,new MainNavigateTabBar.TabParam(R.mipmap.tab_home,R.mipmap.tab_home_select,TAG_PAGE_HOME));
        mainNavigateTabBar.addTab(ShopFragment.class,new MainNavigateTabBar.TabParam(R.mipmap.tab_shop,R.mipmap.tab_shop_select,TAG_PAGE_SHOP));
        mainNavigateTabBar.addTab(null,new MainNavigateTabBar.TabParam(0,0,TAG_PAGE_ADD));
        mainNavigateTabBar.addTab(GuideFragment.class,new MainNavigateTabBar.TabParam(R.mipmap.tab_guide,R.mipmap.tab_guide_select,TAG_PAGE_GUIDE));
        mainNavigateTabBar.addTab(MineFragment.class,new MainNavigateTabBar.TabParam(R.mipmap.tab_mine,R.mipmap.tab_mine_select,TAG_PAGE_MINE));
    }

    private void initView() {
        //隐藏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.hide();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mainNavigateTabBar.onSaveInstanceState(outState);
    }

    //使用PopuoWindow
    public void onClickPublish(View v) {
        if (mPopWindow !=null
                && mPopWindow.isShowing()
                ){
            mPopWindow.dismiss();
            mPopWindow = null;
        } else {
            showPopupWindow();
        }
    }

    //popuowindow弹出框
    private void showPopupWindow() {
        View contentView = LayoutInflater.from(HomeActivity.this).inflate(R.layout.popupwindow,null);
        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        Button btn1= (Button) contentView.findViewById(R.id.pop_order);
        Button btn2= (Button) contentView.findViewById(R.id.pop_sign);
        Button btn3= (Button) contentView.findViewById(R.id.pop_cash);
        Button btn4= (Button) contentView.findViewById(R.id.pop_vote);

        //按钮点击处理事件
        btn1.setOnClickListener((View.OnClickListener) this);
        btn2.setOnClickListener((View.OnClickListener) this);
        btn3.setOnClickListener((View.OnClickListener) this);
        btn4.setOnClickListener((View.OnClickListener) this);

        //显示popupWindow
        View rootView = LayoutInflater.from(HomeActivity.this).inflate(R.layout.activity_home,null);
        //创建PopWindow实例，100,100是宽度和高度
        mPopWindow.showAtLocation(rootView, Gravity.BOTTOM,120,130);
    }


    @Override
    public void onClick(View view) {

    }
}
